global class AccountWebService {
    WebService static String createAccOwnerChangeThread(String ids) {
        String rtn = '';
        // 没有选取客户
        if (String.isBlank(ids) == true) {
            rtn = System.Label.Err_AccNotSelected;
            return rtn;
        }
        
      
        // 权限不够，只有经理/主管才能点
        User u = [select Profile.Name, SubCompanyManager__c, DepartmentManager__c from User where Id = :UserInfo.getUserId()];
        Set<String> pnSet = new Set<String>(System.Label.Profile_CanCreateAccOwnerChange.split(','));
        if (pnSet.contains(u.Profile.Name) == false) {
            rtn = System.Label.Err_CantCreateAccOwnerChange;
            return rtn;
        }

        List<Id> idList = ids.split(',');
        List<Account> accList = [select Id, OwnerChangeProc__c,IFuserule__c, Name, OwnerId from Account where Id in :idList];
        Set<String> Owners = new Set<String>();
        for (Account acc : accList) {
            Owners.add(acc.OwnerId);
            // 如果已经有别的审批在用了，报错
            if (String.isBlank(acc.OwnerChangeProc__c) == false) {
                rtn = System.Label.Err_AccInChangeProc + acc.Name;
                return rtn;
            }
                   //判断要修改的客户的onwer是不是唯一的
            if(Owners.size()>1){
                return '您所选择的客户:"'+acc.Name+'"与其他的客户OnwerId不同,不能批量操作!建议您分开提交审批!';
            }
        }
        //判断操作人有没有权限
        Map<String ,String> right = OnwerRightHandler.isHaveRight(accList);
        System.debug(right);
		if(right.get('success') == 'false'){
        	return '您没有权限操作的客户:' + right.get('msg');
        }        
        try {
             // 新建一个审批的抬头
            Account_Owner_Change__c aoc = new Account_Owner_Change__c();
            aoc.SubCompanyManager__c = u.SubCompanyManager__c;
            aoc.DepartmentManager__c = u.DepartmentManager__c;
            aoc.Status__c = '草案中';
            insert aoc;
        
            for (Account acc : accList) {
                acc.OwnerChangeProc__c = aoc.Id;
                acc.IFuserule__c = !acc.IFuserule__c;
                
            }
            
            update accList;
            
            return aoc.Id;
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}